package pogvue.gui;

import pogvue.analysis.AlignmentUtil;
import pogvue.datamodel.SequenceI;
import pogvue.gui.schemes.ColourSchemeI;

import java.awt.*;
import java.util.Hashtable;
import java.util.Vector;

public final class KmerRenderer implements RendererI {
    final Color purple = new Color(255,50,255);

    public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i) {
	Color c = cs.findColour(seq,seq.getSequence(i,i+1),i,null);
	return c;
    }
    
    public void drawSequence(Graphics g,ColourSchemeI cs,SequenceI seq,int start, int end, int x1, int y1, double width, int height,boolean showScores, boolean displayBoxes, boolean displayText,Vector kmers, int seqnum,AlignViewport av, Hashtable props,int intpid[][]) {
	

	int length = seq.getLength();

	Color currentColor = Color.white;

	g.setColor(Color.black);

        int prev = -1;
	
	int seqno = -1;

	for (int ii = 0; ii < av.getAlignment().getHeight(); ii++) {
	    if (av.getSelection().contains(av.getAlignment().getSequenceAt(ii))) {
		seqno = ii;
	    }
	}

	int blstart = start - 20;
	int blend   = end + 20;

	if (blstart < 0) {
	    blstart = 0;
	}

	if (blend > av.getAlignment().getWidth()) {
	    blend = av.getAlignment().getWidth();
	}
        Hashtable hkmer    = AlignmentUtil.findKmers(av.getAlignment().getSequenceAt(seqnum),blstart,blend,av.getKmers());


        System.out.println("Found kemrs " + hkmer);

        SequenceI topseq = av.getAlignment().getSequenceAt(0);

	int       bstart = -1;
	int       blen   = -1;

	Color bc;
	int i      = blstart;

	SequenceI tmpseq = av.getAlignment().getSequenceAt(seqnum);

	while (i <= blend && i < length-1) {

	    if (tmpseq.getLength() > i+1 &&  topseq.getLength() > i+1) {
	    if (!tmpseq.getSequence().substring(i,i+1).equals("-")) {
                if (topseq.getSequence().substring(i,i+1).equals(tmpseq.getSequence().substring(i,i+1))) {                
		  g.setColor(Color.gray);
                } else {
		  g.setColor(Color.pink);
                }
		g.fillRect(x1+(int)(width*(i-start)),y1,
			   (int)width+1,height/2);
		g.setColor(Color.white);
	    }
	    
	    if (hkmer.get(new Integer(i-blstart)) != null) {

		int klen   = (Integer) hkmer.get(new Integer(i - blstart));
                if (topseq.getSequence().substring(i,i+klen).equals(tmpseq.getSequence().substring(i,i+klen))) {
                   g.setColor(Color.black);
                } else {
		   g.setColor(Color.lightGray);
                }

		g.fillRect(x1+(int)((width*(i-start))),y1+height/2,
			   (int)(width*klen)+1,             height/2);
	    }


		       
	    if (width > 5) {
		g.setColor(Color.black);
		g.drawString(seq.getSequence().substring(i,i+1),x1+(int)(width*(i-start)),y1+height/2);
	    }
	    }
	    i++;
	}
    }

}
